package com.bijiawang.user.controller;

import com.bijiawang.user.bean.Userinfo;
import com.bijiawang.user.repository.FavoritesRepository;
import com.bijiawang.user.repository.NfavoritesRepository;
import com.bijiawang.user.service.FavoritesService;
import com.bijiawang.user.service.HistoryService;
import com.bijiawang.user.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;



@RequestMapping("/change")
@Controller
public class ChangeInfoController {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    UserinfoService userinfoService;

    @Autowired
    HistoryService historyService;

    @Autowired
    FavoritesService favoritesService;

    @ResponseBody
    @GetMapping("/checkCode/{code}/{email}")
    public String checkCode(@PathVariable("code")String code,@PathVariable("email")String email){
        String s = stringRedisTemplate.opsForValue().get(email);
        if (s != null) {
            if (s.equals(code)) {
                return "true";
            }else {
                return "false";
            }
        }else {
            return "false";
        }
    }



    @GetMapping("/forgetpasw")
    public String changepasw(Model model){
        String text = "忘记密码";
        String text1 = "手机号";
        String title = "修改密码";
        String uri = "/change/tochangepasw";
        String name = "number";
        model.addAttribute("text",text);
        model.addAttribute("text1",text1);
        model.addAttribute("title",title);
        model.addAttribute("uri",uri);
        model.addAttribute("name",name);
        return "changeInfo";
    }

    @GetMapping("/tochangepasw")
    public String tochangepasw1(@AuthenticationPrincipal UserDetails userDetails,Model model){
        String username = userDetails.getUsername();
        Userinfo userinfoByUserName = userinfoService.getUserinfoByUserName(username);
        String number = userinfoByUserName.getPhonenumber();
        String title = "修改密码";
        String text = "修改密码";
        String text1 = "手机号";
        String text2 = "密码：";
        String text3 = "确认密码：";
        String type = "password";
        String uri = "/change/updatepasw";
        String name1 = "number";
        String name2 = "pasw";
        model.addAttribute("number",number);
        model.addAttribute("text",text);
        model.addAttribute("text1",text1);
        model.addAttribute("title",title);
        model.addAttribute("type",type);
        model.addAttribute("uri",uri);
        model.addAttribute("text2",text2);
        model.addAttribute("text3",text3);
        model.addAttribute("name1",name1);
        model.addAttribute("name2",name2);
        return "changeInfo2";
    }


    @PostMapping("/tochangepasw")
    public String tochangepasw(@RequestParam("number")String number,Model model){
        String title = "修改密码";
        String text = "修改密码";
        String text1 = "手机号";
        String text2 = "密码：";
        String text3 = "确认密码：";
        String type = "password";
        String uri = "/change/updatepasw";
        String name1 = "number";
        String name2 = "pasw";
        model.addAttribute("number",number);
        model.addAttribute("text",text);
        model.addAttribute("text1",text1);
        model.addAttribute("text2",text2);
        model.addAttribute("text3",text3);
        model.addAttribute("title",title);
        model.addAttribute("type",type);
        model.addAttribute("uri",uri);
        model.addAttribute("name1",name1);
        model.addAttribute("name2",name2);
        return "changeInfo2";
    }

    @PostMapping("/updatepasw")
    public String updatepasw(@RequestParam("number")String number,@RequestParam("pasw")String pasw){
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(pasw);
        userinfoService.updatepasw(number,encode);
        return "login";
    }



    @GetMapping("/username")
    public String username(@AuthenticationPrincipal UserDetails userDetails , Model model){
        String username = userDetails.getUsername();
        String title = "修改用户名";
        String text = "修改用户名";
        String text1 = "用户名";
        String text2 = "新的用户名：";
        String type = "text";
        String uri = "/change/updateUsername";
        String name1 = "oldUsername";
        String name2 = "newUsername";
        model.addAttribute("title",title);
        model.addAttribute("text",text);
        model.addAttribute("text1",text1);
        model.addAttribute("number",username);
        model.addAttribute("text2",text2);
        model.addAttribute("type",type);
        model.addAttribute("uri",uri);
        model.addAttribute("name1",name1);
        model.addAttribute("name2",name2);
        return "changeInfo2";
    }

    @ResponseBody
    @PostMapping("/updateUsername")
    public void updateUsername(@RequestParam("newUsername")String newUsername,@RequestParam("oldUsername")String oldUsername){
//        favoritesService.updateUsername(oldUsername,newUsername);
        userinfoService.updateUsername(oldUsername,newUsername);
        historyService.updateUsername(oldUsername,newUsername);
    }

    @GetMapping("/phoneNumber")
    public String phoneNumber(@AuthenticationPrincipal UserDetails userDetails,Model model){
        String username = userDetails.getUsername();
        Userinfo userinfoByUserName = userinfoService.getUserinfoByUserName(username);
        String number = userinfoByUserName.getPhonenumber();
        String text = "修改手机号";
        String text1 = "手机号";
        String title = "修改手机号";
        String uri = "/change/tochangeNumber";
        String name = "number";
        String readonly = "true";
        model.addAttribute("text",text);
        model.addAttribute("text1",text1);
        model.addAttribute("title",title);
        model.addAttribute("uri",uri);
        model.addAttribute("name",name);
        model.addAttribute("readonly",readonly);
        model.addAttribute("number",number);
        return "changeInfo";
    }

    @PostMapping("/tochangeNumber")
    public String tochangeNumber(@AuthenticationPrincipal UserDetails userDetails,Model model){
        String username = userDetails.getUsername();
        Userinfo userinfoByUserName = userinfoService.getUserinfoByUserName(username);
        String number = userinfoByUserName.getPhonenumber();
        String title = "修改手机号";
        String text = "修改手机号";
        String text1 = "手机号";
        String text2 = "新的手机号：";
        String type = "text";
        String uri = "/change/updatePhoneNumber";
        String name1 = "oldPhoneNumber";
        String name2 = "newPhoneNumber";
        model.addAttribute("number",number);
        model.addAttribute("text",text);
        model.addAttribute("text1",text1);
        model.addAttribute("text2",text2);
        model.addAttribute("title",title);
        model.addAttribute("type",type);
        model.addAttribute("uri",uri);
        model.addAttribute("name1",name1);
        model.addAttribute("name2",name2);
        return "changeInfo2";
    }


    @PostMapping("/updatePhoneNumber")
    public String phoneNumber(@RequestParam("oldPhoneNumber")String oldPhoneNumber,@RequestParam("newPhoneNumber")String newPhoneNumber){
        userinfoService.updatePhoneNumber(oldPhoneNumber,newPhoneNumber);
        return "redirect:/ToUserInfo/" + newPhoneNumber;
    }

    @GetMapping("/Email")
    public String changeEmail(@AuthenticationPrincipal UserDetails userDetails, Model model){
        String username = userDetails.getUsername();
        Userinfo userinfoByUserName = userinfoService.getUserinfoByUserName(username);
        String email = userinfoByUserName.getEmail();
        String title = "修改邮箱";
        String text = "修改邮箱";
        String text1 = "现在邮箱";
        String text2 = "新的邮箱：";
        String type = "text";
        String uri = "/change/updateEmail";
        String name1 = "username";
        String name2 = "email";
        model.addAttribute("title",title);
        model.addAttribute("text",text);
        model.addAttribute("text1",text1);
        model.addAttribute("number",email);
        model.addAttribute("text2",text2);
        model.addAttribute("type",type);
        model.addAttribute("uri",uri);
        model.addAttribute("name1",name1);
        model.addAttribute("name2",name2);
        return "changeInfo2";
    }

    @PostMapping("/updateEmail")
    public String updateEmail(@RequestParam("email")String email,@RequestParam("username")String username){
        userinfoService.updateEmail(email,username);
        return "redirect:/ToUserInfo/" + username;
    }
}
